package com.opennetexam.manager.sqlprovider;

import com.opennetexam.manager.databasemodel.ExamineeExamPaper;
import com.opennetexam.manager.utils.GsonUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 奶茶
 * Created on 2017/11/14.
 */
public class ExamineeExamPaperProvider {

    private static Logger logger = Logger.getLogger(ExamineeExamPaperProvider.class);

    public String getExamineeExamPaperCountBySomeWhere(Map<String,Object> parameter){
        StringBuilder stringBuffer = new StringBuilder();
        String appId = (String) parameter.get("appId");
        String examPlanInfoId = (String) parameter.get("examPlanInfoId");
        String examSubjectId = (String) parameter.get("examSubjectId");

        stringBuffer.append("SELECT COUNT(*) FROM ExamineeExamPaper ");
        stringBuffer.append("WHERE AppID = \'").append(appId).append("\' AND ExamPlanInfoID = \'").append(examPlanInfoId).append("\' AND ExamSubjectID = \'").append(examSubjectId).append("\' ");

        String sql = stringBuffer.toString();
        logger.info(sql);
        return sql;
    }

    public String deleteExamineeExamPaper(Map<String,Object> parameter){
        StringBuilder sb = new StringBuilder("");
        sb.append("DELETE FROM ExamineeExamPaper WHERE 1=1 AND ExamPaperInfoID IN(");
        List<ExamineeExamPaper> examPapers = GsonUtil.fromJsonToList(GsonUtil.toJson(parameter.get("examPapers")), ExamineeExamPaper.class);
        for (int i=0;i<examPapers.size();i++){
            if(i != (examPapers.size() - 1)){
                sb.append("\'").append(examPapers.get(i).getExamPaperInfoID()).append("\',");
            }else{
                sb.append("\'").append(examPapers.get(i).getExamPaperInfoID()).append("\')");
            }
        }

        String sql = sb.toString();
        logger.info(sql);
        return sql;
    }

    public static void main(String[] args){
        Map<String,Object> map = new HashMap<>();
        map.put("appId","1");
        map.put("examPlanInfoId",66);
        map.put("examSubjectId",246);

        List<ExamineeExamPaper> examineeExamPapers = new ArrayList<>();
        ExamineeExamPaper examineeExamPaper = new ExamineeExamPaper();
        examineeExamPaper.setExamPaperInfoID("1");
        examineeExamPapers.add(examineeExamPaper);
        examineeExamPapers.add(examineeExamPaper);

        map.put("examPapers",examineeExamPapers);

//        deleteExamineeExamPaper(map);

    }
}
